<?php
namespace modules\potential;
use \modules\ui;
use \modules\auth\api as auth;
use \phiction\array_utils as arr;
use \modules\contact\api as contact_api;
use \modules\contact\storage as contact;
use \modules\user\storage as user;

class page
{
    static function mapping()
    {
        return [
            '/add/#id' => 'add',
            '/show/#id' => 'show',
            '/edit/#id' => 'edit',
            '/active/#page' => 'active',
            '/archived/#page' => 'archived',
            '/list/#id' => 'list_all',
            '/delete/#id' => 'confirm',
            '/myself/#page'=>'list_my',
        ];
    }

    static function add($q, $html, &$args)
    {
        $user = auth::validate_with_role($q, $args, 'salesman');
        $orgname = $user['orgname'];
        $responsible = $user['name'];

        $contact =  contact_api::contact_profile($q, $args);
        $contact_id = $contact['id'];
        $contact_name = $contact['name'];

        $args['SUBTITLE'] = "添加商机";
        $args['HEADER-CONTROL'] = ui::header_control_with_logout($q, $args, $html);

        return ui::card(ui::form($html, '/api/potential/add',
            ui::icon_label('添加商机', 'plus'), [
                ['hidden'  , ''        , $contact_id , 'name' => 'contact-id'],
                ['text'    , '隶属组织', $orgname, 'readonly' => true],
                ['text'    , '负责人'  , $responsible, 'readonly' => true],
                ['text'    , '联系人'  , $contact_name, 'readonly' => true],
                ['text'    , '说明'    , 'name' => 'name', 'maxlength' => 64],
                ['int'     , '优先级'  , 0, 'name' => 'priority'],
                ['longtext', '备注'    , null, true, 'name' => 'comment'],
            ], [
                [ui::icon_label('返回联系人资料', 'angle-left'), "/contact/show/$contact_id"],
            ], "/contact/show/$contact_id"),
        "添加商机");
    }

    static function active($q, $html, &$args)
    {
        $page = arr::get_or_bad_request($args, 'page');
        $list = api::list_active($q, $args);

        $args['SUBTITLE'] = "有效商机";
        $args['HEADER-CONTROL'] = ui::header_control_with_logout($q, $args, $html);

        foreach ($list as &$item) {
            $id       = $item['id'];
            $name     = $item['name'];
            $priority = $item['priority'];
            $responsible = $item['responsible'];
            $contact  = $item['contact'];
            $item = [
                "$name 优先级（{$priority}）", "负责人 $responsible", "href" => "/potential/show/$id",
                [
                    [ui::icon_label('修改商机', 'edit'), "/potential/edit/$id"],
                    [ui::icon_label('关闭', 'ban'), "data-api" => "/api/potential/archive", "data-api-id" => $id],
                ],
            ];
        }

        $pager = ui::pager(function ($page) {
            return "/potential/active/$page";
        }, $page, count($list) === 10);

        return ui::card(['.', ui::list($list), $pager], "有效商机");
    }

    static function archived($q, $html, &$args)
    {
        $page = arr::get_or_bad_request($args, 'page');
        $list = api::list_archived($q, $args);

        $args['SUBTITLE'] = "已关闭商机";
        $args['HEADER-CONTROL'] = ui::header_control_with_logout($q, $args, $html);

        foreach ($list as &$item) {
            $id       = $item['id'];
            $name     = $item['name'];
            $priority = $item['priority'];
            $responsible = $item['responsible'];
            $contact  = $item['contact'];
            $item = [
                "$name （已关闭）", "负责人 $responsible", "href" => "/potential/show/$id",
                [
                    [ui::icon_label('修改商机', 'edit'), "/potential/edit/$id"],
                    [ui::icon_label('恢复', 'check-circle-o'), "data-api" => "/api/potential/active", "data-api-id" => $id],
                ],
            ];
        }

        $pager = ui::pager(function ($page) {
            return "/potential/archived/$page";
        }, $page, count($list) === 10);

        return ui::card(['.', ui::list($list), $pager], "已关闭商机");
    }

    static function show($q, $html, &$args)
    {
        $user    = auth::validate($q, $args);
        $org     = $user['org'];
        $orgname = $user['orgname'];

        $profile      = api::potential_profile($q, $args);
        $name         = $profile['name'];
        $responsible_id = $profile['responsible'];
        $contact_id   = $profile['contact'];
        $created      = $profile['created'];
        $comment      = $profile['comment'];
        $id           = $profile['id'];
        $active       = $profile['active'];

        $responsible  = user::profile($q, $org, $responsible_id)->run();
        $responsible_name = $responsible['name'];

        $contact      = contact::profile($q, $org, $contact_id)->run();
        $contact_name = $contact['name'];
        $contact_phone= $contact['phone1'];
        $contact_addr = $contact['addr1'];
        $contact_birthdate = $contact['birthdate'];

        $args['SUBTITLE'] = "商机资料";
        $args['HEADER-CONTROL'] = ui::header_control_with_logout($q, $args, $html, [
            [ui::icon_label('联系人资料', 'info'), "/contact/show/$contact_id"],
            [ui::icon_label('修改商机', 'edit'), "/potential/edit/$id"],
        ]);

        return ui::card(ui::readonly_form($html,
            [
                ['text'     , '隶属组织'  , $orgname],
                ['text'     , '说明'      , $name]   ,
                ['text'     , '负责人'    , $responsible_name],
                ['text'     , '联系人'    , $contact_name],
                ['text'     , '联系人电话', $contact_phone],
                ['text'     , '联系人地址', $contact_addr],
                ['date'     , '联系人生日', $contact_birthdate],
                ['date'     , '创建时间'  , $created],
                ['longtext' , '商机备注'  , $comment],
                ['enum'     , '状态'      , [$active ? 't' : 'f', 't' => '有效', 'f' => '已关闭'], 'name' => 'active'] ,
            ]), "商机 $name 的资料");
    }

    static function edit($q, $html, &$args)
    {
        $user = auth::validate_with_role($q, $args, 'salesman');
        $responsible_name = $user['name'];
        $org     = $user['org'];
        $orgname = $user['orgname'];

        $profile      = api::potential_profile($q, $args);
        $name         = $profile['name'];
        $responsible_id = $profile['responsible'];
        $contact_id   = $profile['contact'];
        $comment      = $profile['comment'];
        $id           = $profile['id'];
        $priority     = $profile['priority'];

        $responsible  = user::profile($q, $org, $responsible_id)->run();
        $responsible_prename = $responsible['name'];

        $contact      = contact::profile($q, $org, $contact_id)->run();
        $contact_name = $contact['name'];

        $args['SUBTITLE'] = "修改商机";
        $args['HEADER-CONTROL'] = ui::header_control_with_logout($q, $args, $html,[
            [ui::icon_label('修改联系人', 'edit'), "/contact/edit/$contact_id"],
        ]);

        return ui::card(ui::form($html, '/api/potential/edit',
            ui::icon_label('修改商机', 'edit'), [
                ['hidden', ''        , $id                 , 'name' => 'id'],
                ['hidden', ''        , $contact_id         , 'name' => 'contact-id'],
                ['text'  , '隶属组织', $orgname            , 'readonly' => true],
                ['text'  , '原负责人', $responsible_prename, 'readonly' => true],
                ['text'  , '现负责人', $responsible_name   , 'readonly' => true],
                ['text'  , '联系人'  , $contact_name       , 'readonly' => true],
                ['text'  , '说明'    , $name               , 'name' => 'name', 'maxlength' => 64],
                ['int'   , '优先级'  , $priority           , 'name' => 'priority'],
                ['longtext', '商机备注', $comment          , 'name' => 'comment', true],
            ], [
                [ui::icon_label('返回商机资料', 'angle-left'), "/potential/show/$id"],
            ], "/potential/show/$id"),
        "修改商机");
    }

    static function list_all($q, $html, &$args)
    {
        $contact = (int)arr::get_or_bad_request($args, 'id');
        $list = api::list_by_contact($q, $args);

        $args['SUBTITLE'] = "所有商机";
        $args['HEADER-CONTROL'] = ui::header_control_with_logout($q, $args, $html, [
            [ui::icon_label('添加商机', 'plus'), "/potential/add/$contact"],
        ]);

        foreach ($list as &$item) {
            $id     = $item['id'];
            $name   = $item['name'];
            $priority = $item['priority'];
            $responsible = $item['responsible'];
            $active = $item['active'];
            $status = $active ? "优先级（{$priority}）" : "（已关闭）";
            $item = [
                "$name $status", "负责人 $responsible", "href" => "/potential/show/$id",
                [
                    [ui::icon_label('修改商机', 'edit'), "/potential/edit/$id"],
                    $item['active']
                        ? [ui::icon_label('关闭', 'ban'           ), "data-api" => "/api/potential/archive", "data-api-id" => $id]
                        : [ui::icon_label('恢复', 'check-circle-o'), "data-api" => "/api/potential/active" , "data-api-id" => $id],
                ],
            ];
        }
        return ui::card(['.', ui::list($list)], "该联系人的所有商机");
    }

    static function list_my($q, $html, &$args)
    {
        $page = arr::get_or_bad_request($args, 'page');
        $list = api::list_my_potential($q, $args);

        $args['SUBTITLE'] = "我负责的商机";
        $args['HEADER-CONTROL'] = ui::header_control_with_logout($q, $args, $html);

        foreach ($list as &$item) {
            $id       = $item['id'];
            $name     = $item['name'];
            $priority = $item['priority'];
            $contact_phone  = $item['contact-phone'];
            $contact_name   = $item['contact-name'];
            $contact_gender =['man' => '先生', 'woman' => '女士'][$item['contact-gender']];
            $active   = $item['active'];
            $status   = $active ? "优先级（{$priority}）" : "（已关闭）";
            $item = [
                "$name $status","$contact_phone $contact_name $contact_gender", "href" => "/potential/show/$id",
                [
                    [ui::icon_label('修改商机', 'edit'), "/potential/edit/$id"],
                    $item['active']
                        ? [ui::icon_label('关闭', 'ban'           ), "data-api" => "/api/potential/archive", "data-api-id" => $id]
                        : [ui::icon_label('恢复', 'check-circle-o'), "data-api" => "/api/potential/active" , "data-api-id" => $id],
                ],
            ];
        }
        $pager = ui::pager(function ($page) {
            return "/potential/myself/$page";
        }, $page, count($list) === 10);

        return ui::card(['.', ui::list($list), $pager], "我负责的商机");
    }
}

